package com.java.basis.base64;

import org.hibernate.exception.spi.ViolatedConstraintNameExtracter;
import org.junit.Test;

import java.io.*;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.Base64;
import java.util.List;

public class Base64Test {
    @Test
    public void test01() throws IOException {
        String filePath = "C:\\Users\\wangning\\Desktop\\我是utf_8格式的文件.txt";

        byte[] bytes = Files.readAllBytes(Paths.get(filePath));
        System.out.println("bytes = " + Arrays.toString(bytes));
        String s = Base64.getEncoder().encodeToString(bytes);
        System.out.println(s);

    }

    @Test
    public void test011() throws IOException {
        String filePath = "C:\\Users\\wangning\\Desktop\\base64.txt";
        String s = fileToBase64(filePath);
        System.out.println("s = " + s);

//        List<String> lines = Files.readAllLines(Paths.get(filePath), Charset.forName("GBK"));

//        for (String line : lines) {
//            byte[] bytes = line.getBytes(StandardCharsets.UTF_8);
//            System.out.println(bytes);
//            String s = Base64.getEncoder().encodeToString(bytes);
//            System.out.println(s);

//        }

//        String s = Base64.getEncoder().encodeToString(bytes);
//        System.out.println(s);

    }

    @Test
    public void test02() throws Exception {
        InputStreamReader isr = new InputStreamReader(new FileInputStream("C:\\Users\\wangning\\Desktop\\base64.txt"), "GBK");
        //2.创建OutputStreamWriter对象,构造方法中传递字节输出流和指定的编码表名称UTF-8
        OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("C:\\Users\\wangning\\Desktop\\我是utf_8格式的文件.txt"), "UTF-8");
        //3.使用InputStreamReader对象中的方法read读取文件
        int len = 0;
        while ((len = isr.read()) != -1) {
            //4.使用OutputStreamWriter对象中的方法write,把读取的数据写入到文件中
            osw.write(len);
        }
        //5.释放资源
        osw.close();
        isr.close();
    }


    @Test
    public void test03() throws Exception {
        final Base64.Decoder decoder = Base64.getDecoder();
        final Base64.Encoder encoder = Base64.getEncoder();
        final String text = "字串文字";
        final byte[] textByte = text.getBytes("UTF-8");
        //编码
        final String encodedText = encoder.encodeToString(textByte);
        System.out.println(encodedText);
        //解码
        System.out.println(new String(decoder.decode(encodedText), "UTF-8"));
    }

    public static String fileToBase64(String path) {
        String base64 = null;
        InputStream in = null;
        try {
            File file = new File(path);
            in = new FileInputStream(file);
            byte[] bytes = new byte[(int) file.length()];
            in.read(bytes);

            base64 = Base64.getEncoder().encodeToString(bytes);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (in != null) {
                try {
                    in.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return base64;
    }
}
